package org.jeecg.modules.crawler.service.impl;

import org.jeecg.modules.crawler.entity.CueCrawlerInfo;
import org.jeecg.modules.crawler.mapper.CueCrawlerInfoMapper;
import org.jeecg.modules.crawler.service.ICueCrawlerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 爬虫信息
 * @Author: jeecg-boot
 * @Date:   2020-05-02
 * @Version: V1.0
 */
@Service
public class CueCrawlerInfoServiceImpl extends ServiceImpl<CueCrawlerInfoMapper, CueCrawlerInfo> implements ICueCrawlerInfoService {

    @Autowired
    CueCrawlerInfoMapper cueCrawlerInfoMapper;
    @Override
    public Map<String, Object> initCrawlerData() {
        List<String> keywordList = cueCrawlerInfoMapper.initKeyword();
        String keywordStr = "";
        if(null != keywordList){
           for (int i=0;i<keywordList.size();i++){
               keywordStr += (keywordList.get(i)+"#");
           }
        }
        Map<String,Object> rules = cueCrawlerInfoMapper.initRule();
        rules.put("keyword",keywordStr);
        return rules;
    }

    @Override
    public void keywordOk(String keyword) {
        String[] keywordArray = keyword.split("#");
        cueCrawlerInfoMapper.clearKeyword();
        cueCrawlerInfoMapper.setKeyword(keywordArray);
    }

    @Override
    public void ruleOk(Map<String, Object> paramMap) {
        cueCrawlerInfoMapper.ruleOk(paramMap);
        //组装规则语言
        String rule = "";
        if(!StringUtils.isEmpty(paramMap.get("forwardStr"))){
            rule +="转发量大与"+paramMap.get("forwardStr") + "|";
        }

        if(!StringUtils.isEmpty(paramMap.get("remarkStr"))){
            rule +="评论量大与"+paramMap.get("remarkStr")+ "|";
        }

        if(!StringUtils.isEmpty(paramMap.get("readStr"))){
            rule +="阅读量大与"+paramMap.get("readStr") + "|";
        }

        if(!StringUtils.isEmpty(paramMap.get("timeStr"))){
            rule +="抓取时间范围为"+paramMap.get("timeStr") + "小时内|";
        }

//        rule = rule.substring(rule.length()-1,rule.length());
        cueCrawlerInfoMapper.updateCrawlerRule(rule);




    }

    @Override
    public void changeCrawlerState(String id, String state) {
        cueCrawlerInfoMapper.changeCrawlerState(id,state);

    }

    @Override
    public List<Map<String, Object>> querySpiderLog(String id) {
        return cueCrawlerInfoMapper.querySpiderLog(id);
    }
}
